import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'
import Redirect from '@/views/redirect/index'
import Login from '@/views/login/index'
import AuthRedirect from '@/views/login/auth-redirect'
import ErrorPage404 from '@/views/error-page/404'
import ErrorPage401 from '@/views/error-page/401'
import Dashboard from '@/views/dashboard/index'
import Document from '@/views/documentation/index'
import Guide from '@/views/guide/index'
import Profile from '@/views/profile/index'
import PagePermission from '@/views/permission/page'
import DirectivePermission from '@/views/permission/directive'
import RolePermission from '@/views/permission/role'
import Icons from '@/views/icons/index'
import CreateArticle from '@/views/example/create'
import EditArticle from '@/views/example/edit'
import ArticleList from '@/views/example/list'
import Tab from '@/views/tab/index'
import Errorlog from '@/views/error-log/index'
import ExportExcel from '@/views/excel/export-excel'
import SelectExcel from '@/views/excel/select-excel'
import MergeHeader from '@/views/excel/merge-header'
import UplaodExcel from '@/views/excel/upload-excel'
import ExportZip from '@/views/zip/index'
import PDF from '@/views/pdf/index'
import DownloadPdf from '@/views/pdf/download'
import Theme from '@/views/theme/index'
import Register from '@/views/register/index'
/* Router Modules */
// import componentsRouter from './modules/components'
// import chartsRouter from './modules/charts'
// import tableRouter from './modules/table'
// import nestedRouter from './modules/nested'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'             the icon show in the sidebar
    noCache: true                if set true, the page will no be cached(default is false)
    affix: true                  if set true, the tag will affix in the tags-view
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    id: '0',
    path: '/register',
    component: Register,
    hidden: true
  },
  {
    id: '1',
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        id: '2',
        path: '/redirect/:path*',
        component: Redirect
      }
    ]
  },
  {
    id: '3',
    path: '/login',
    component: Login,
    hidden: true
  },
  {
    id: '4',
    path: '/auth-redirect',
    component: AuthRedirect,
    hidden: true
  },
  {
    id: '5',
    path: '/404',
    component: ErrorPage404,
    hidden: true
  },
  {
    id: '6',
    path: '/401',
    component: ErrorPage401,
    hidden: true
  },
  {
    id: '7',
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        id: '8',
        path: 'dashboard',
        component: Dashboard,
        name: 'Dashboard',
        meta: { title: 'Dashboard', icon: 'dashboard', affix: true }
      }
    ]
  },
  {
    id: '9',
    path: '/documentation',
    component: Layout,
    children: [
      {
        id: '10',
        path: 'index',
        component: Document,
        name: 'Documentation',
        meta: { title: 'Documentation', icon: 'documentation', affix: true }
      }
    ]
  },
  {
    id: '11',
    path: '/guide',
    component: Layout,
    redirect: '/guide/index',
    children: [
      {
        id: '12',
        path: 'index',
        component: Guide,
        name: 'Guide',
        meta: { title: 'Guide', icon: 'guide', noCache: true }
      }
    ]
  },
  {
    id: '13',
    path: '/profile',
    component: Layout,
    redirect: '/profile/index',
    hidden: true,
    children: [
      {
        id: '14',
        path: 'index',
        component: Profile,
        name: 'Profile',
        meta: { title: 'Profile', icon: 'user', noCache: true }
      }
    ]
  },
  { id: '48', path: 'user/logout', name: 'logout', hidden: true },
  { id: '49', path: '*', name: 'notFound', redirect: '/404', hidden: true }
]

export const allRouteComponentMap = {
  '@/layout': Layout,
  '@/views/redirect/index': Redirect,
  '@/views/login/index': Login,
  '@/views/login/auth-redirect': AuthRedirect,
  '@/views/error-page/404': ErrorPage404,
  '@/views/error-page/401': ErrorPage401,
  '@/views/dashboard/index': Dashboard,
  '@/views/documentation/index': Document,
  '@/views/guide/index': Guide,
  '@/views/profile/index': Profile,
  '@/views/permission/page': PagePermission,
  '@/views/permission/directive': DirectivePermission,
  '@/views/permission/role': RolePermission,
  '@/views/icons/index': Icons,
  '@/views/example/create': CreateArticle,
  '@/views/example/edit': EditArticle,
  '@/views/example/list': ArticleList,
  '@/views/tab/index': Tab,
  '@/views/error-log/index': Errorlog,
  '@/views/excel/export-excel': ExportExcel,
  '@/views/excel/select-excel': SelectExcel,
  '@/views/excel/merge-header': MergeHeader,
  '@/views/excel/upload-excel': UplaodExcel,
  '@/views/zip/index': ExportZip,
  '@/views/pdf/index': PDF,
  '@/views/pdf/download': DownloadPdf,
  '@/views/theme/index': Theme
}

export const asyncRoutes = [
  {
    id: '15',
    path: '/permission',
    component: Layout,
    redirect: '/permission/page',
    alwaysShow: true, // will always show the root menu
    name: 'Permission',
    meta: {
      title: '权限管理',
      icon: 'lock'
    //  roles: ['admin', 'editor']
    },
    children: [
      {
        id: '16',
        path: 'page',
        component: () => import('@/views/permission/page'),
        name: 'PagePermission',
        meta: {
          title: '用户管理'
        }
      },
      {
        id: '17',
        path: 'directive',
        component: () => import('@/views/permission/directive'),
        name: 'DirectivePermission',
        meta: {
          title: 'Directive Permission'
          // if do not set roles, means: this page does not require permission
        }
      },
      {
        id: '18',
        path: 'role',
        component: () => import('@/views/permission/role'),
        name: 'RolePermission',
        meta: {
          title: '角色管理'
          // roles: ['admin']
        }
      }
    ]
  },
  {
    id: '19',
    path: '/icon',
    name: 'Icons',
    component: Layout,
    children: [
      {
        id: '20',
        path: 'index',
        component: () => import('@/views/icons/index'),
        name: 'Icons',
        meta: { title: 'Icons', icon: 'icon', noCache: true }
      }
    ]
  },
  // componentsRouter,
  // chartsRouter,
  // nestedRouter,
  // tableRouter,
  {
    id: '21',
    path: '/example',
    component: Layout,
    redirect: '/example/list',
    name: 'Example',
    meta: {
      title: 'Example',
      icon: 'example'
    },
    children: [
      {
        id: '22',
        path: 'create',
        component: () => import('@/views/example/create'),
        name: 'CreateArticle',
        meta: { title: 'Create Article', icon: 'edit' }
      },
      {
        id: '23',
        path: 'edit/:id(\\d+)',
        component: () => import('@/views/example/edit'),
        name: 'EditArticle',
        meta: { title: 'Edit Article', noCache: true, activeMenu: '/example/list' },
        hidden: true
      },
      {
        id: '24',
        path: 'list',
        component: () => import('@/views/example/list'),
        name: 'ArticleList',
        meta: { title: 'Article List', icon: 'list' }
      }
    ]
  },
  {
    id: '25',
    path: '/tab',
    component: Layout,
    name: 'tab',
    children: [
      {
        id: '26',
        path: 'index',
        component: () => import('@/views/tab/index'),
        name: 'Tab',
        meta: { title: 'Tab', icon: 'tab' }
      }
    ]
  },
  {
    id: '27',
    path: '/error',
    component: Layout,
    redirect: 'noRedirect',
    name: 'ErrorPages',
    meta: {
      title: 'Error Pages',
      icon: '404'
    },
    children: [
      {
        id: '28',
        path: '401',
        component: () => import('@/views/error-page/401'),
        name: 'Page401',
        meta: { title: '401', noCache: true }
      },
      {
        id: '29',
        path: '404',
        component: () => import('@/views/error-page/404'),
        name: 'Page404',
        meta: { title: '404', noCache: true }
      }
    ]
  },

  {
    id: '30',
    path: '/error-log',
    name: 'error-log',
    component: Layout,
    children: [
      {
        id: '31',
        path: 'log',
        component: () => import('@/views/error-log/index'),
        name: 'ErrorLog',
        meta: { title: 'Error Log', icon: 'bug' }
      }
    ]
  },

  {
    id: '32',
    path: '/excel',
    component: Layout,
    redirect: '/excel/export-excel',
    name: 'Excel',
    meta: {
      title: 'Excel',
      icon: 'excel'
    },
    children: [
      {
        id: '33',
        path: 'export-excel',
        component: () => import('@/views/excel/export-excel'),
        name: 'ExportExcel',
        meta: { title: 'Export Excel' }
      },
      {
        id: '34',
        path: 'export-selected-excel',
        component: () => import('@/views/excel/select-excel'),
        name: 'SelectExcel',
        meta: { title: 'Export Selected' }
      },
      {
        id: '35',
        path: 'export-merge-header',
        component: () => import('@/views/excel/merge-header'),
        name: 'MergeHeader',
        meta: { title: 'Merge Header' }
      },
      {
        id: '36',
        path: 'upload-excel',
        component: () => import('@/views/excel/upload-excel'),
        name: 'UploadExcel',
        meta: { title: 'Upload Excel' }
      }
    ]
  },

  {
    id: '37',
    path: '/zip',
    component: Layout,
    redirect: '/zip/download',
    alwaysShow: true,
    name: 'Zip',
    meta: { title: 'Zip', icon: 'zip' },
    children: [
      {
        id: '38',
        path: 'download',
        component: () => import('@/views/zip/index'),
        name: 'ExportZip',
        meta: { title: 'Export Zip' }
      }
    ]
  },

  {
    id: '39',
    path: '/pdf',
    name: 'pdf',
    component: Layout,
    redirect: '/pdf/index',
    children: [
      {
        id: '40',
        path: 'index',
        component: () => import('@/views/pdf/index'),
        name: 'PDF',
        meta: { title: 'PDF', icon: 'pdf' }
      }
    ]
  },
  {
    id: '41',
    path: '/pdf/download',
    name: 'download',
    component: () => import('@/views/pdf/download'),
    hidden: true
  },

  {
    id: '42',
    path: '/theme',
    name: 'theme',
    component: Layout,
    children: [
      {
        id: '43',
        path: 'index',
        component: () => import('@/views/theme/index'),
        name: 'Theme',
        meta: { title: 'Theme', icon: 'theme' }
      }
    ]
  },

  {
    id: '44',
    path: '/clipboard',
    name: 'clipboard',
    component: Layout,
    children: [
      {
        id: '45',
        path: 'index',
        component: () => import('@/views/clipboard/index'),
        name: 'ClipboardDemo',
        meta: { title: 'Clipboard', icon: 'clipboard' }
      }
    ]
  },
  {
    id: '46',
    path: 'external-link',
    name: 'external-link',
    component: Layout,
    children: [
      {
        id: '47',
        path: 'https://github.com/PanJiaChen/vue-element-admin',
        meta: { title: 'External Link', icon: 'link' }
      }
    ]
  }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
